import localforage from 'localforage/dist/localforage.js';
import { getInfo } from '@/utils/auth';
/* eslint no-extend-native: ["error", { "exceptions": ["Array"] }] */
Object.defineProperty(Array.prototype, 'unique2', {
enumerable: false,
configurable: false,
writable: false,
value: function () {
    this.sort(); // 先排序
        if (this.length === 0) return [];
        let res = [this[0]];
        for (let i = 1; i < this.length; i++) {
            if (this[i] !== res[res.length - 1]) {
                res.push(this[i]);
            }
        }
    return res;
}
});

let headerdata = [];
let titledata;
let resultdatalist;
let title1default = [];
// 字段树 数据
export function headerDataFiltering(header_list) {
    if (!header_list) {
        return;
    }
     titledata = header_list.filter(function (v, i) {
        return !(v.layout && v.layout.dim_input === 'checkbox' && v.title_en === '');
    });
     headerdata = header_list.filter(function (v, i) {
        return !(v.layout && v.layout.dim_input === 'checkbox' && v.title_en === '');
    });

    resultdatalist = titledata.filter(function (v, i) {
        return (v.id.split('_').length === 3 || v.id.split('_').length === 4) && !(v.layout.dim_input === 'checkbox' && v.title_en === '');
    });

    for (let i = 0; i < titledata.length; i++) {
        if (titledata[i].pid === '0') {
            titledata[i].nocheck = true;
        }
        if (titledata[i].title_default === 1) {
            if (titledata[i].id.split('_').length === 3 || titledata[i].id.split('_').length === 4) {
                title1default.push(titledata[i].id);
            }
            titledata[i].checked = true;
        } else {
            titledata[i].checked = false;
        }
    }

    Array.prototype.unique2(title1default);

    for (let i = 0; i < title1default.length; i++) {
            for (let j = 0; j < titledata.length; j++) {
                if (titledata[j].id === title1default[i].replace(/_\d$/, '')) {
                    titledata[j].checked = true;
                }
            }
    }
    return { headerdata, titledata, resultdatalist, title1default };
}
const userInfo = getInfo();
const userTableName = userInfo.username + userInfo.uid;
// console.log(userTableName);
/**
 *
 *表格缓存要区分账号用户和和不用病种缓存
 * @export function
 * @param {String}} projectId //病种id
 * @param {String} lastUpdateTime //缓存上传更新事件
 * @returns
 */
export function tableInfo(projectId, lastUpdateTime) {
    // 获取字段列表中的表格字段
    let tableFieldDataDb = localforage.createInstance({
        name: 'tableFieldDataDb',
        storeName: 'tableFieldDataDbTable'
    });
    return new Promise(function (resolve, reject) {
        tableFieldDataDb.getItem(`projectId_${projectId}`).then((value) => {
            console.log(value);
            let tableinfo = {};
            let tablelist = [];
            tableinfo.last_update_time = lastUpdateTime;
            for (let i = 0; i < titledata.length; i++) {
                if (titledata[i].id.split('_').length === 3 && titledata[i].layout.dim_input === 'table') {
                    tablelist.push({
                        table_en: titledata[i].title_en,
                        table_cn: titledata[i].title_name,
                        table_id: titledata[i].id,
                        table_fields: [],
                        data_select: []
                    });
                }
            }
            if (!value) {
                tableinfo[userTableName] = tablelist;
                tableFieldDataDb.setItem(`projectId_${projectId}`, tableinfo);
                resolve(tablelist);
            } else {
                if (value[userTableName] === undefined || value.last_update_time !== lastUpdateTime) {
                    tableinfo[userTableName] = tablelist;
                    tableFieldDataDb.setItem(`projectId_${projectId}`, tableinfo);
                    resolve(tablelist);
                } else {
                    resolve(value[userTableName]);
                }
            }
        });
    });
}
